package mx.vlabs.soapdroid.wsdl2java.ui;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileFilter;

import org.ow2.easywsdl.schema.api.ComplexType;
import org.ow2.easywsdl.schema.api.SimpleType;
import org.ow2.easywsdl.wsdl.api.Description;
import org.ow2.easywsdl.wsdl.api.InterfaceType;
import org.ow2.easywsdl.wsdl.api.Service;
import org.ow2.easywsdl.wsdl.api.WSDLException;


public class WSClientGeneratorPanel extends WizardPanel {
	private JTextField txtProjectLocation;
	private JTextField txtPackageName;
	private JFileChooser fcProjectLocation;
	private DefaultListModel<String> lstWsdlDefModel,
									 lstServicesModel,
									 lstPortTypesModel,
									 lstDTOsModel,
									 lstEnumTypesModel;
	private JScrollPane spWsdlDef,spServices,spPortTypes,spDTOs,spEnumTypes;
	private JTabbedPane tabbedPane;
	

	public JTextField getTxtProjectLocation() {
		return txtProjectLocation;
	}



	public JTextField getTxtPackageName() {
		return txtPackageName;
	}



	/**
	 * Create the panel.
	 */
	public WSClientGeneratorPanel() {
		super();
		
		fcProjectLocation = new JFileChooser();
		
		fcProjectLocation.setDialogTitle("Android Project Location");
		fcProjectLocation.setFileFilter(new FileFilter() {
			
			@Override
			public String getDescription() {
				// TODO Auto-generated method stub
				return "Directories only";
			}
			
			@Override
			public boolean accept(File arg0) {
				// TODO Auto-generated method stub
				return arg0.isDirectory();
			}
		});
		
		fcProjectLocation.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				txtProjectLocation.setText(fcProjectLocation.getSelectedFile().getAbsolutePath());
			}
		});
		
		
		
		fcProjectLocation.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		
		spWsdlDef = new JScrollPane();
		spWsdlDef.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		spServices = new JScrollPane();
		spServices.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		spPortTypes = new JScrollPane();
		spPortTypes.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		spDTOs = new JScrollPane();
		spDTOs.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		spEnumTypes = new JScrollPane();
		spEnumTypes.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

		
		JPanel pnlServiceDefinition = new JPanel();
		pnlServiceDefinition.setBorder(new TitledBorder(new LineBorder(new Color(105, 105, 105), 1, true), "Service Definition", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JPanel pnlOutputOptions = new JPanel();
		pnlOutputOptions.setBorder(new TitledBorder(new LineBorder(new Color(105, 105, 105), 1, true), "Output Options", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(pnlOutputOptions, GroupLayout.DEFAULT_SIZE, 520, Short.MAX_VALUE)
						.addComponent(pnlServiceDefinition, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 520, Short.MAX_VALUE))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(pnlServiceDefinition, GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(pnlOutputOptions, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
		);
		
		tabbedPane = new JTabbedPane(JTabbedPane.BOTTOM);
		
		
		GroupLayout gl_pnlServiceDefinition = new GroupLayout(pnlServiceDefinition);
		gl_pnlServiceDefinition.setHorizontalGroup(
			gl_pnlServiceDefinition.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_pnlServiceDefinition.createSequentialGroup()
					.addContainerGap()
					.addComponent(tabbedPane, GroupLayout.DEFAULT_SIZE, 490, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_pnlServiceDefinition.setVerticalGroup(
			gl_pnlServiceDefinition.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_pnlServiceDefinition.createSequentialGroup()
					.addContainerGap()
					.addComponent(tabbedPane, GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
					.addContainerGap())
		);
		
		lstWsdlDefModel = new DefaultListModel<String>();
		
		JList lstWsdlDef = new JList();
		lstWsdlDef.setBorder(new EmptyBorder(5, 5, 5, 5));
		lstWsdlDef.setModel(lstWsdlDefModel);
		
		lstWsdlDef.setBackground(Color.WHITE);
		
		spWsdlDef.setViewportView(lstWsdlDef);
		
		tabbedPane.addTab("WSDL Definition", null, spWsdlDef, null);
		
		lstServicesModel = new DefaultListModel<String>();
		
		JList lstServices = new JList();
		lstServices.setBorder(new EmptyBorder(5, 5, 5, 5));
		lstServices.setModel(lstServicesModel);
		lstServices.setBackground(Color.WHITE);
		
		spServices.setViewportView(lstServices);
		
		tabbedPane.addTab("Services", null, spServices, null);
		
		lstPortTypesModel = new DefaultListModel<String>();
		
		JList lstPortTypes = new JList();
		lstPortTypes.setBorder(new EmptyBorder(5, 5, 5, 5));
		lstPortTypes.setModel(lstPortTypesModel);
		lstPortTypes.setBackground(Color.WHITE);
		
		spPortTypes.setViewportView(lstPortTypes);
		
		tabbedPane.addTab("Port Types", null, spPortTypes, null);
		
		lstDTOsModel = new DefaultListModel<String>();
		
		JList lstDTOs = new JList();
		lstDTOs.setBorder(new EmptyBorder(5, 5, 5, 5));
		lstDTOs.setModel(lstDTOsModel);
		lstDTOs.setBackground(Color.WHITE);
		
		spDTOs.setViewportView(lstDTOs);
		
		tabbedPane.addTab("Complex Types (DTOs)", null, spDTOs, null);
		
		lstEnumTypesModel = new DefaultListModel<String>();
		
		JList lstEnumTypes = new JList();
		lstEnumTypes.setBorder(new EmptyBorder(5, 5, 5, 5));
		lstEnumTypes.setModel(lstEnumTypesModel);
		lstEnumTypes.setBackground(Color.WHITE);
		
		spEnumTypes.setViewportView(lstEnumTypes);
		
		tabbedPane.addTab("Simple Types (Enums)", null, spEnumTypes, null);
		pnlServiceDefinition.setLayout(gl_pnlServiceDefinition);
		
		
		JLabel lblProjectLocation = new JLabel("Project Folder:");
		
		txtProjectLocation = new JTextField();
		txtProjectLocation.setColumns(10);
		
		JButton btnBrowse = new JButton("Browse...");
		btnBrowse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				fcProjectLocation.showOpenDialog(getParentFrame());
				
			}
		});
		
		txtPackageName = new JTextField();
		txtPackageName.setColumns(10);
		
		JLabel lblPackageName = new JLabel("Package Name:");
		GroupLayout gl_pnlOutputOptions = new GroupLayout(pnlOutputOptions);
		gl_pnlOutputOptions.setHorizontalGroup(
			gl_pnlOutputOptions.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_pnlOutputOptions.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_pnlOutputOptions.createParallelGroup(Alignment.TRAILING)
						.addComponent(lblProjectLocation)
						.addComponent(lblPackageName))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_pnlOutputOptions.createParallelGroup(Alignment.LEADING)
						.addComponent(txtProjectLocation, GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE)
						.addComponent(txtPackageName, GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnBrowse)
					.addContainerGap())
		);
		gl_pnlOutputOptions.setVerticalGroup(
			gl_pnlOutputOptions.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_pnlOutputOptions.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_pnlOutputOptions.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblProjectLocation)
						.addComponent(txtProjectLocation, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnBrowse, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_pnlOutputOptions.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtPackageName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblPackageName))
					.addContainerGap(16, Short.MAX_VALUE))
		);
		pnlOutputOptions.setLayout(gl_pnlOutputOptions);
		setLayout(groupLayout);
	}

	public DefaultListModel<String> getLstWsdlDefModel() {
		return lstWsdlDefModel;
	}



	public DefaultListModel<String> getLstServicesModel() {
		return lstServicesModel;
	}



	public DefaultListModel<String> getLstPortTypesModel() {
		return lstPortTypesModel;
	}



	public DefaultListModel<String> getLstDTOsModel() {
		return lstDTOsModel;
	}
	
	



	public DefaultListModel<String> getLstEnumTypesModel() {
		return lstEnumTypesModel;
	}



	@Override
	public void init() {
		// TODO Auto-generated method stub
		loadWSDLDescriptionData();
		loadWSDLServicesData();
		loadWSDLPortTypesData();
		loadWSDLDTOsData();
		loadEnumTypesData();
		tabbedPane.setSelectedIndex(0);
		
		txtPackageName.setText(getParentFrame().getWsdl2Java().getPackageName());
	}
	
	private void loadWSDLDescriptionData(){
		
		getLstWsdlDefModel().clear();
		
		Description desc = getParentFrame().getWsdl2Java().getDescription();
		
		getLstWsdlDefModel().addElement("Url: "+desc.getDocumentBaseURI());
		getLstWsdlDefModel().addElement("Target Namespace: "+desc.getTargetNamespace());
		getLstWsdlDefModel().addElement("Version: "+desc.getVersion().name());

		
	}
	
	private void loadWSDLServicesData(){
		
		getLstServicesModel().clear();
		
		for(Service service : getParentFrame().getWsdl2Java().getServices()){
			getLstServicesModel().addElement(service.getQName().toString());
		}
	}
	
	private void loadWSDLPortTypesData(){
		getLstPortTypesModel().clear();
		
		for(InterfaceType interfaceType : getParentFrame().getWsdl2Java().getInterfaces()){
			getLstPortTypesModel().addElement(interfaceType.getQName().toString());
		}
	}
	
	private void loadWSDLDTOsData(){
		
		getLstDTOsModel().clear();
		
		for(ComplexType complexType : getParentFrame().getWsdl2Java().getComplexTypes()){
			getLstDTOsModel().addElement(complexType.getQName().toString());
		}
	}

	private void loadEnumTypesData(){
		
		getLstEnumTypesModel().clear();
		
		for(SimpleType simpleType : getParentFrame().getWsdl2Java().getSimpleTypes()){
			getLstEnumTypesModel().addElement(simpleType.getQName().toString());
		}
	}
	

}
